package com.foreknow.demo15;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

public class Demo {
    /**
     * 统计子字符串出现的次数
     * @param line
     */
    public static void subParse(String line) {
        String subLine = null;
        List<String> list = new ArrayList<>();
        Map<String,Integer> map = new TreeMap<>();
        for(int i = 0;i<line.length();i++) {
            for(int j = i + 1;j<=line.length();j++){
                //截取子串
                subLine =  line.substring(i,j);
                System.out.println(subLine);
                //将子串保存到List中，并将子串以k,v保存到Map中
                if(subLine.length()!=line.length()){
                    list.add(subLine);
                    map.put(subLine,0);
                }
            }
        }

        for(int i = 0;i<list.size();i++){
            for (Map.Entry<String,Integer> entry : map.entrySet()) {
                if(list.get(i).equals(entry.getKey())) {
                    entry.setValue(entry.getValue()+1);
                }
            }
        }

        for(Map.Entry<String,Integer> entry : map.entrySet()) {
                System.out.println(entry.getKey()+"--------"+entry.getValue());
                System.out.println();
        }
    }



    public static void main(String[] args) {
        subParse("abcabc");
    }
}
